﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Zero.UControl.Component.Notification
{
    /// <summary>
    /// ToastNotification.xaml 的交互逻辑
    /// </summary>
    public partial class ToastNotification : UserControl
    {
        private readonly DispatcherTimer _closeTimer;
        // 当点击该消息提示时执行附加的逻辑
        private event EventHandler _notifyEvent;
        public event EventHandler Closed;

        public ToastNotification()
        {
            InitializeComponent();

            _closeTimer = new DispatcherTimer();
            _closeTimer.Interval = TimeSpan.FromSeconds(3); // 显示时间
            _closeTimer.Tick += (s, e) => Close();
        }

        public void Show(string title, string message, int durationSeconds = 3,EventHandler notifyEvent = null)
        {
            this.Show(title, message, null, durationSeconds, notifyEvent);
        }

        public void Show(string title, string message,FrameworkElement element, int durationSeconds = 3, EventHandler notifyEvent = null)
        {
            TitleText.Text = title;
            MessageText.Text = message;
            if(element != null)
                ContentCtrl.Content = element;
            _notifyEvent = notifyEvent;
            BeginFadeIn();
            _closeTimer.Interval = TimeSpan.FromSeconds(durationSeconds);
            _closeTimer.Start();
        }

        private void BeginFadeIn()
        {
            var trans = new TranslateTransform();
            this.RenderTransform = trans;

            var animation = new DoubleAnimation
            {
                From = 340,
                To = 0,
                Duration = TimeSpan.FromMilliseconds(300),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
            };
            trans.BeginAnimation(TranslateTransform.XProperty, animation);
        }

        private void BeginFadeOut()
        {
            var trans = new TranslateTransform();
            this.RenderTransform = trans;

            var animation = new DoubleAnimation
            {
                From = 0,
                To = 340,
                Duration = TimeSpan.FromMilliseconds(300),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
            };
            animation.Completed += (s, e) =>
            {
                Closed?.Invoke(this, EventArgs.Empty);
            };
            trans.BeginAnimation(TranslateTransform.XProperty, animation);
        }


        private void Close()
        {
            _closeTimer.Stop();
            BeginFadeOut();
        }

        #region 事件
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _notifyEvent?.Invoke(this, null);
            this.Close();
        }

        private void Close_Btn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Border_MouseEnter(object sender, MouseEventArgs e)
        {
            _closeTimer.Stop();
        }

        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            _closeTimer.Start();
        }
        #endregion
    }
}
